<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>AutoBox handleToJSON</title>
</head>

<body>
  <script>
    /**
     * 尝试利用参数来处理 JSON
     * 方案失败
     * 原因：get 请求不能获取过长参数
     */

    // 获取参数
    function getQueryVariable(variable) {
      const query = window.location.search.substring(1)
      const vars = query.split("&")
      for (var i = 0; i < vars.length; i++) {
        let pair = vars[i].split("=")
        if (pair[0] == variable) { return decodeURIComponent(pair[1]) }
      }
      return (false)
    }

    const opt = getQueryVariable("opt")
    const key = getQueryVariable("key")
    const json = getQueryVariable("json")
    const setting = getQueryVariable("setting")
    document.write(json)
    console.log('hello world')


    const obj = JSON.parse(json.replace(/\n/g, "\\n"))
    if (opt == 0) {
      if (!obj.task) {
        obj.task = []
      }
      obj.stock = (obj.stock instanceof Array) ? obj.stock : [obj.stock];
      obj.task.push(JSON.parse(setting.replace(/\n/g, "\\n")))
      obj.stock.splice(key - 1, 1)
    }
    if (opt == 1) {
      if (!obj.stock) {
        obj.stock = []
      }
      obj.stock.push(JSON.parse(setting.replace(/\n/g, "\\n")))
      obj.task.splice(key - 1, 1)
    }
    document.write(JSON.stringify(obj))
  </script>
</body>

</html>